#pragma once
#include "oj_model2.hpp"
#include <ctemplate/template.h>

namespace ns_view
{
    using namespace ns_model;
    class View
    {
        const std::string templatePath = "./template/";
    public:
        View()
        {}
        ~View()
        {}
        void AllExpandHtml(std::vector<Question>& all,std::string *html)
        {
            //1.获取模板网页路径
            std::string in_html = templatePath + "all_questions.html";
            //2.形成数据字典
            ctemplate::TemplateDictionary root("all_questions");
            //设置子字典，渲染网页表格
            for(auto & q:all)
            {
                ctemplate::TemplateDictionary * td =root.AddSectionDictionary("questions_list");
                td->SetValue("number",q._number);
                td->SetValue("title",q._title);
                td->SetValue("star",q._level);
            }
            //3.获取待渲染网页
            ctemplate::Template *tql = ctemplate::Template::GetTemplate(in_html,ctemplate::DO_NOT_STRIP);
            //4.渲染网页
            tql->Expand(html,&root);
        }
        void OneExpandHtml(Question &q,std::string *html)
        {
            std::string in_html = templatePath + "one_question.html";
            ctemplate::TemplateDictionary root ("one_question");
            root.SetValue("number",q._number);
            root.SetValue("title",q._title);
            root.SetValue("star",q._level);
            root.SetValue("desc",q._desc);
            root.SetValue("pre_code",q._header);
            ctemplate::Template *tql = ctemplate::Template::GetTemplate(in_html,ctemplate::DO_NOT_STRIP);
            tql->Expand(html,&root);
        }

    };
}